﻿using System.Security.Cryptography;
using System.Text;

namespace yongGu
{
    public static class AesUtil
    {
        const string SECRETKEY = "nAnlUmaIygdieDa37234566543410000";
        public static string AesEncrypt(string encryptStr)
        {
            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptStr);
            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(SECRETKEY);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = aes.CreateEncryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Convert.ToBase64String(Encoding.UTF8.GetBytes(Convert.ToBase64String(resultArray, 0, resultArray.Length)));
            }
        }
        public static string AesDecrypt(string decryptStr)
        {
            Byte[] toEncryptArray = Convert.FromBase64String(Encoding.UTF8.GetString(Convert.FromBase64String(decryptStr)));

            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(SECRETKEY);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = aes.CreateDecryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Encoding.UTF8.GetString(resultArray);
            }
        }
    }
}
